package dao

import (
	"fmt"
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/util/gconv"
)

var T_hzfgw_node_para = t_hzfgw_node_para{}

type t_hzfgw_node_para struct {
}

func (a *t_hzfgw_node_para) GetHnum(addr string, sn string) (hnum string, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("t_hzfgw_node_para")
	res, error := model.Where(fmt.Sprintf(" addr='%s' and sn='%s' ", addr, sn)).Fields("hnum").One()
	if error != nil {
		err = error
	} else {
		if res != nil {
			hnum = gconv.String(res.Map()["hnum"])
		}
	}
	return hnum, err
}

func (a *t_hzfgw_node_para) GetTn(sn string) (tn int, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("t_hzfgw_node_para")
	res, error := model.Where(fmt.Sprintf(" sn='%s' ", sn)).Fields("tn").One()
	if error != nil {
		err = error
		tn = -1
	} else {
		if res != nil {
			tn = gconv.Int(res.Map()["tn"])
		}
	}
	return tn, err
}

func (a *t_hzfgw_node_para) GetPigNum(addr string, sn string) (pig_num int, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("t_hzfgw_node_para")
	res, error := model.Where(fmt.Sprintf(" addr='%s' and sn='%s' ", addr, sn)).Fields("pig_num").One()
	if error != nil {
		err = error
	} else {
		if res != nil {
			pig_num = gconv.Int(res.Map()["pig_num"])
		}
	}
	return pig_num, err
}

func (a *t_hzfgw_node_para) Exits(addr string, sn string) (exits bool, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("t_hzfgw_node_para")
	result, error := model.Where(fmt.Sprintf(" addr='%s' and sn='%s' ", addr, sn)).Count()
	if error != nil {
		err = error
	} else {
		if result > 0 {
			exits = true
		} else {
			exits = false
		}
	}
	return exits, err
}

func (a *t_hzfgw_node_para) GetList(where ...string) (list gdb.List, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("t_hzfgw_node_para")
	if len(where) == 0 {
	} else {
		model = model.Where(where[0])
	}
	res, err := model.All()
	return res.List(), err
}

func (a *t_hzfgw_node_para) Save(data interface{}) (isOk bool, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	_, err = db.Model("t_hzfgw_node_para").Data(data).Save()
	if err != nil {
		return false, err
	} else {
		return true, nil
	}
}

func (a *t_hzfgw_node_para) Clear(addr string) (isOk bool, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	_, err = db.Model("t_hzfgw_node_para").Delete(fmt.Sprintf(" addr='%s' ", addr))
	if err != nil {
		return false, err
	} else {
		return true, nil
	}
}

func (a *t_hzfgw_node_para) DeleteNode(sn string) (isOk bool, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	_, err = db.Model("t_hzfgw_node_para").Delete(fmt.Sprintf(" sn='%s' ", sn))
	if err != nil {
		return false, err
	} else {
		return true, nil
	}
}
